<template>
  <div class="common-search">
    <BaseType v-if="isShowType" v-bind="$attrs" ref="BaseType" :navList="navList"></BaseType>
    <el-form ref="searchForm" :model="values" inline @submit.native.prevent>
      <BaseFormItem
        v-for="item in config"
        class="base-search__form-item"
        :key="item.prop"
        v-model="values[item.prop]"
        v-bind="item"
      />
      <slot name="footer">
        <el-form-item v-if="$route.name === 'statisticsManagementList'" class="common-search__footer">
          <template
            v-if="!(config.find(obj => obj.prop === 'statisticalDay') || config.find(obj => obj.label === '选择月份'))"
          >
            <span @click="reset">重置</span>
            <span @click="submit">搜索</span>
          </template>
        </el-form-item>
        <el-form-item class="common-search__footer" v-else>
          <span @click="reset">重置</span>
          <span @click="submit">搜索</span>
        </el-form-item>
        <el-form-item class="common-search__footer" v-if="$route.name === 'statisticsManagementList'">
          <span @click="exportsFile">导出</span>
        </el-form-item>
      </slot>
    </el-form>
  </div>
</template>

<script>
import BaseFormItem from './form-item.vue'
import BaseType from './type.vue'

export default {
  name: 'BaseSearch',
  inheritAttrs: false,
  components: {
    BaseFormItem,
    BaseType,
  },
  props: {
    // 是否展示所属类型 -- 目前仅产品管理搜索栏需要
    isShowType: {
      type: Boolean,
      default: false,
    },
    navList: {
      type: Array,
      default: () => [],
    },
    value: null,
    config: {
      type: Array,
      required: true,
      default: () => [],
    },
    onSubmit: Function,
    onInput: Function,
  },
  watch: {
    config: {
      handler: 'initValues',
      immediate: true,
      deep: true,
    },
  },
  computed: {
    values: {
      get() {
        return this.value || {}
      },

      set(val) {
        if (this.onInput) {
          this.onInput(val)
        }

        this.$emit('input', val)
      },
    },
  },
  methods: {
    initValues() {
      this.values = this.config.reduce((values, item) => {
        values[item.prop] = item.value
        return values
      }, {})
    },
    exportsFile() {
      this.$emit('exports')
    },
    submit() {
      const values = { ...this.values }

      if (this.onSubmit) {
        this.onSubmit(values)
      }

      this.$emit('submit', values)
    },
    reset() {
      if (this.isShowType && this.$refs.BaseType.onSwitchType) {
        this.$refs.BaseType.onSwitchType(0, '')
      }
      this.$refs.searchForm.resetFields()
      this.submit()
    },
  },
}
</script>
<style lang="scss" scoped>
.common-search__footer {
  span {
    display: inline-block;
    text-align: center;
    border-radius: 1px;
    width: 136px;
    height: 36px;
    line-height: 36px;
    cursor: pointer;
    &:first-child {
      border: 1px solid $blue;
      color: $blue;
      margin-right: 10px;
    }
    &:last-child {
      background: $blue;
      color: $white;
      border: 1px solid transparent;
    }
  }
}
::v-deep {
  .base-search__form-item {
    margin-right: 40px;
  }
  .el-input {
    width: 260px;
  }
}
</style>
